package com.yc.chaoshi.cs_manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.yc.chaoshi.cs_manage.DTO.AdminDTO;
import com.yc.chaoshi.cs_manage.mapper.UmsRolesMapper;
import com.yc.chaoshi.cs_manage.service.IUmsRolesResourceService;
import com.yc.chaoshi.cs_manage.service.IUmsRolesService;
import com.yc.chaoshi.entity.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xz
 * @since 2022-08-04
 */
@Service
public class UmsRolesServiceImpl extends ServiceImpl<UmsRolesMapper, UmsRoles> implements IUmsRolesService {
    @Resource
    IUmsRolesResourceService rolesResourceService;
    /**
     * 根据名字查询所有角色
     * @param pageNum
     * @param pageSize
     * @param sreachname
     * @return
     */
    @Override
    public Page listAll(Integer pageNum, Integer pageSize, String sreachname) {
        Page<AdminDTO> page=new Page<>(pageNum,pageSize);
        pageNum=pageSize*(pageNum-1);
        List<AdminDTO> list=this.baseMapper.listAll(pageNum,pageSize,sreachname);
        long total=this.baseMapper.listAll(0,10000,sreachname).size();
        page.setRecords(list);
        page.setTotal(total);
        return page;
    }

    /**
     * 修改权限管理员是否启用状态
     * @param id
     * @param status
     * @return
     */
    @Override
    public boolean updatestatus(Integer id, Integer status) {
            UpdateWrapper<UmsRoles> updateWrapper=new UpdateWrapper<>();
            updateWrapper.lambda().eq(UmsRoles::getId,id)
                    .set(UmsRoles::getStatus,status);
            return this.update(updateWrapper);
    }

    @Override
    public Boolean edit(UmsRoles umsRoles) {
        if (this.updateById(umsRoles)){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public Boolean add(UmsRoles umsRoles) {
        umsRoles.setCreateTime(LocalDateTime.now());
        boolean result=this.save(umsRoles);
        if (result){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public Boolean addResource(List<Integer> ids, Integer roleid) {
        QueryWrapper<UmsRolesResource> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(UmsRolesResource::getRolesId,roleid);
        rolesResourceService.remove(queryWrapper);
        List<UmsRolesResource> list=new ArrayList<>();
        for (Integer id:ids) {
            UmsRolesResource rolesResource=new UmsRolesResource();
            rolesResource.setResourceId(id);
            rolesResource.setRolesId(roleid);
            list.add(rolesResource);
        }
        return rolesResourceService.saveBatch(list);
    }


}
